package com.zhangtai.modules.service.impl.contract;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.constVo.ContractConst;
import com.zhangtai.modules.dao.ContractMangerDao;
import com.zhangtai.modules.dto.ContractNewDto;
import com.zhangtai.modules.dto.ContractRenewDto;
import com.zhangtai.modules.dto.ContractTerminateDto;
import com.zhangtai.modules.entity.contract.ContractMangerEntity;
import com.zhangtai.modules.service.contract.ContractMangerService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@RestController
public class ContractMangerServiceImpl extends ServiceImpl<ContractMangerDao, ContractMangerEntity> implements ContractMangerService {
    @Autowired
    private ContractMangerDao contractMangerDao;
    @Override
    public PageResult queryPage(@RequestParam(value = "oid", required = false) String oid,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<ContractMangerEntity> page = this.page(
                new Page<ContractMangerEntity>(pageNum,pageSize),
                new QueryWrapper<ContractMangerEntity>().like(!StringUtils.isEmpty(oid),"oid",oid)
                        .eq("is_delete",0)
        );

        return new PageResult(page);
    }

    @Override
    public PageResult<ContractMangerEntity> getList(@RequestParam(value = "contractType", required = false) String contractType,
                              @RequestParam(value = "contractStatus", required = false) Integer contractStatus,
                              @RequestParam(value = "operateType", required = false) Integer operateType,
                              @RequestParam(value = "type", required = false) Integer type,
                              @RequestParam(value = "orgIds", required = false) List<Long> orgIds,
                              @RequestParam(value = "jobName", required = false) String jobName,
                              @RequestParam(value = "unitName", required = false) String unitName,
                               @RequestParam(value = "hireDate", required = false) Date hireDate,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<ContractMangerEntity> page = this.page(
                new Page<ContractMangerEntity>(pageNum,pageSize),
                new QueryWrapper<ContractMangerEntity>().like(!StringUtils.isEmpty(contractType),"contract_type",contractType)
                        .eq(!ObjectUtils.isEmpty(contractStatus),"contract_status",contractStatus)
                        .eq(!ObjectUtils.isEmpty(operateType),"contract_type",operateType)
                        .eq(!ObjectUtils.isEmpty(type),"type",type)
                        .in(!CollectionUtils.isEmpty(orgIds),"unit_id",orgIds)
                        .eq(CollectionUtils.isEmpty(orgIds),"unit_id",null)
                        .like(!StringUtils.isEmpty(jobName),"job_name",jobName)
                        .like(!StringUtils.isEmpty(unitName),"unit_name",unitName)
                        .ge(!ObjectUtils.isEmpty(hireDate),"hire_date",hireDate)
                        .eq("is_delete",0)
                        .orderByDesc("crate_time")
        );

        return new PageResult<ContractMangerEntity>(page);
    }

    public PageResult<ContractNewDto> getNewList(@RequestParam(value = "code", required = false) String code,
                                                 @RequestParam(value = "name", required = false) String name,
                                                 @RequestParam(value = "jobId", required = false) List<Long> jobId,
                                                 @RequestParam(value = "unitId", required = false) List<Long> unitId,
                                                 @RequestParam(value = "orgId", required = false) List<Long> orgId,
                                                 @RequestParam(value = "hiredate", required = false) String hiredate,
                                                 @RequestParam(value = "contractStatus", defaultValue = "1") List<Integer> contractStatus,
                                                 @RequestParam(value = "orgIds", required = false) List<Long> orgIds,
                                                 @RequestParam(value = "isApply", required = false) boolean isApply,
                                                 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        IPage<ContractNewDto> page = new Page<>(pageNum,pageSize);
        List<ContractNewDto> list = contractMangerDao.getMangerList(name,code,jobId,unitId,orgId,hiredate,contractStatus,orgIds,isApply,page);
        page.setRecords(list);
        return new PageResult<ContractNewDto>(page);
    }

    public List<ContractMangerEntity> getPersonNewList(@RequestParam(value = "code", required = false) String code,
                                                       @RequestParam(value = "contractStatus", defaultValue = "1") List<Integer> contractStatus){
        List<ContractMangerEntity> list = this.list(new QueryWrapper<ContractMangerEntity>().eq("code",code).in("contract_status",contractStatus).orderByAsc("create_time"));
        return list;
    }

    public PageResult<ContractRenewDto> getRenewList(@RequestParam(value = "code", required = false) String code,
                                                 @RequestParam(value = "name", required = false) String name,
                                                 @RequestParam(value = "jobId", required = false) List<Long> jobId,
                                                 @RequestParam(value = "unitId", required = false) List<Long> unitId,
                                                     @RequestParam(value = "orgId", required = false) List<Long> orgId,
                                                 @RequestParam(value = "hiredate", required = false) String hiredate,
                                                     @RequestParam(value = "contractStatus", defaultValue = "1") List<Integer> contractStatus,
                                                     @RequestParam(value = "status", required = false) Integer status,
                                                 @RequestParam(value = "orgIds", required = false) List<Long> orgIds,
                                                     @RequestParam(value = "isApply", required = false) boolean isApply,
                                                 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        IPage<ContractRenewDto> page = new Page<ContractRenewDto>(pageNum,pageSize);
        List<ContractRenewDto> list = contractMangerDao.getContinueMangerList(name,code,jobId,unitId,orgId,hiredate,contractStatus,status,orgIds,isApply,page);
        page.setRecords(list);
        return new PageResult<ContractRenewDto>(page);
    }

    public PageResult<ContractTerminateDto> getTerminateList(@RequestParam(value = "code", required = false) String code,
                                                             @RequestParam(value = "name", required = false) String name,
                                                             @RequestParam(value = "orgId", required = false) List<Long> orgId,
                                                             @RequestParam(value = "unitId", required = false) List<Long> unitId,
                                                             @RequestParam(value = "contractType", required = false) Integer contractType,
                                                             @RequestParam(value = "contractTimeType", required = false) Integer contractTimeType,
                                                             @RequestParam(value = "status", required = false) Integer status,
                                                             @RequestParam(value = "operateType", required = false) Integer operateType,
                                                             @RequestParam(value = "endType", required = false) String endType,
                                                             @RequestParam(value = "orgIds", required = false) List<Long> orgIds,
                                                             @RequestParam(value = "contractStatus", defaultValue = "1") List<Integer> contractStatus,
                                                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                             @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        IPage<ContractTerminateDto> page = new Page<>(pageNum,pageSize);
        List<ContractTerminateDto> list = contractMangerDao.getTerminateList(code,name,orgId,unitId,contractType,
                contractTimeType,operateType,status,orgIds,contractStatus,endType,page);
        page.setRecords(list);

        return new PageResult<ContractTerminateDto>(page);
    }

    @Override
    public ContractMangerEntity getByIdE(Long id) {
            ContractMangerEntity one = this.getOne(new QueryWrapper<ContractMangerEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    public List<ContractMangerEntity> getDrawToCloseList(@RequestParam(value = "date", required = false) String date){
        List<Integer> i = new ArrayList<>();
        i.add(ContractConst.CONTRACT_TYPE_NEW);
        i.add(ContractConst.CONTRACT_TYPE_RENEW);
        i.add(ContractConst.CONTRACT_TYPE_TRANSFER);
        List<ContractMangerEntity> entityList = this.list(new QueryWrapper<ContractMangerEntity>().
                eq("contract_status", ContractConst.EMPLOYEE_STATUS_SIGNED).eq("is_delete",0).
                lt("contract_end_date",date).in("type",i).eq("oid",772777361677619200l));
        return entityList;
    }

    public List<ContractMangerEntity> getDrawToRenewList(@RequestParam(value = "date", required = false) String date){
        List<ContractMangerEntity> entityList = contractMangerDao.getDrawToRenewList(date);
        return entityList;
    }

    @Override
    public ContractMangerEntity getByEsignId(Integer flowId){
        ContractMangerEntity one = this.getOne(new QueryWrapper<ContractMangerEntity>().eq("esign_flow_id", flowId).eq("is_delete", 0));

        return one;
    }

    @Override
    public R saveE(ContractMangerEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(ContractMangerEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            ContractMangerEntity entity = new ContractMangerEntity();
        entity.setId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}